import { _decorator, EventTouch, Node, v2, v3, Vec3 } from 'cc';
import { Ab_Tile_bA } from '../tile';
import { Ab_Drawer_bA } from './drawer';
const { ccclass, property } = _decorator;

@ccclass('Ab_DrawerStash_bA')
export class Ab_DrawerStash_bA extends Ab_Drawer_bA {

	protected onLoad(){
		super.onLoad();
	}

	protected start(){
		this.Ab_left_board_bA = -this.Ab_uitrans_bA.width * this.Ab_uitrans_bA.anchorX;

		this.Ab_tile_size_bA = Ab_Tile_bA.Ab_calculateSizeByBackground_bA(this.Ab_uitrans_bA.width, this.Ab_uitrans_bA.height).block;
		this.Ab_capacity_bA = Math.floor(this.Ab_uitrans_bA.width / this.Ab_tile_size_bA.width);
		this.Ab_padding_x_bA = (this.Ab_uitrans_bA.width - this.Ab_tile_size_bA.width * this.Ab_capacity_bA) / (this.Ab_capacity_bA + 1);
		this.Ab_tween_speed_bA = Math.max(this.Ab_tile_size_bA.width, this.Ab_tile_size_bA.height) / 0.1;

		this.node.on(Node.EventType.TOUCH_END, this.Ab_touchEnd_bA, this);
	}

	private Ab_touch_bA = {
		Ab_location_bA: v2(),
		Ab_position_bA: v3(),
		Ab_world_position_bA: v3(),
	}

	private Ab_touchEnd_bA(event: EventTouch){
		event.getUILocation(this.Ab_touch_bA.Ab_location_bA);
		this.Ab_touch_bA.Ab_world_position_bA.set(this.Ab_touch_bA.Ab_location_bA.x, this.Ab_touch_bA.Ab_location_bA.y, 0);
		this.Ab_uitrans_bA.convertToNodeSpaceAR(this.Ab_touch_bA.Ab_world_position_bA, this.Ab_touch_bA.Ab_position_bA);

		let select_item: typeof this.Ab_record_list_bA[number];
		let distance = v3();
		for(let record of this.Ab_record_list_bA.filter(r=>!this.Ab_adding_tile_bA.find(el=>el.token==r.token))){
			Vec3.subtract(distance, record.tile.node.position, this.Ab_touch_bA.Ab_position_bA);
			if([distance.x/this.Ab_tile_size_bA.width, distance.y/this.Ab_tile_size_bA.height].every(v=>Math.abs(v)<=0.5)){
				select_item = record;
				break;
			}
		}
		distance = null;

		if(select_item){
			Ab_DrawerStash_bA.Ab_select_call_list_bA.forEach(call=>call(select_item));
		}
	}

	private static Ab_select_call_list_bA: ((record: InstanceType<typeof Ab_DrawerStash_bA>['Ab_record_list_bA'][number])=>any)[] = [];
	public static Ab_onSelectRecord_bA(call: (record: InstanceType<typeof Ab_DrawerStash_bA>['Ab_record_list_bA'][number])=>any){
		this.Ab_select_call_list_bA.push(call);
	};

	public Ab_removeRecord_bA(tile: Ab_Tile_bA){
		let record = this.Ab_removeRecordByTile_bA(tile);
		if(record) this.Ab_refresh_bA();
	}
}

